package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSDUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/upload")
@CrossOrigin
public class FileUploadController {

    public Result upload(@RequestParam(value = "file") MultipartFile file) throws Exception {

        //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),  //文件名字
                file.getBytes(),
                StringUtils.getFilenameExtension(file.getOriginalFilename()) //获取扩展名
        );
        //调用fastDFSUtil工具类文件传入fastDFS中
        FastDFSDUtil.upload(fastDFSFile);
        String url = "http://192.168.211.132.8080";
        return new Result(true, StatusCode.OK, "上传成功！");
    }
}
